require 'Common/define'
require 'Logic/Config'
require 'Common/functions'

UICharactorInfoHeroSkill = {};
HelperFunc.SetUIEnv(UICharactorInfoHeroSkill)
local this = UICharactorInfoHeroSkill;

this.currentHeroData = nil;

this.gameObject = nil;
this.transform = nil;
this.LuaBehaviour = nil;

this.hasInit = false;

this.trickGroup = {};
this.specialGroup={};
this.specialTwoGroup ={};
this.passiveGroup = {};
this.skill5Group = {}
this.level = nil

function UICharactorInfoHeroSkill.Init(partnerId)--todo 应该小写 暂时先大写
    currentHeroData = logicMgr.HeroManager.getHeroDatabyPartnerId(partnerId,true);
    local name,package = getUIName(PanelNames.UICharactorInfoHeroSkill);
    panelMgr:ShowUI(name,package, this.onCreate, Config.useLocalUI);
end
---@param obj FairyGUI.GComponent
function UICharactorInfoHeroSkill.onCreate(obj)
    gameObject = obj;
    if not hasInit then
        hasInit = true;
        local temp = {"TrickGroup","SpecailGroup","SpecailTwoGroup","PassiveGroup","skill5"}
        for k,v in pairs(temp) do
            local tb = nil
            if k == 1 then
                tb = trickGroup
            elseif k == 2 then
                tb = specialGroup
            elseif k == 3 then
                tb = specialTwoGroup
            elseif k == 4 then
                tb = passiveGroup
            else
                tb = skill5Group
            end
            tb.btn = obj:GetChild(v)
            tb.icon = tb.btn:GetChild("Mask"):GetChild("Icon")
            tb.objLock = tb.btn:GetController("CtrlLock")
            tb.txtLock = tb.btn:GetChild("LockText")
            tb.objLv = tb.btn:GetController("CtrlLv")
            tb.txtLv = tb.btn:GetChild("SkillLevelText")
            tb.ctrlRace = tb.btn:GetController("race")
            tb.ctrlFifthSkill = tb.btn:GetController("fifthSkill")
            if k == 3 or k == 4 then
                tb.btnSwitch = tb.btn:GetChild("SwitchBtn")
            end
        end
        this.ctrlRace = obj:GetController("race")
        this.ctrlFifthSkill = obj:GetController("fifthSkill")
        this.addEvent();
    end
    this.level =logicMgr.HeroManager.getHeroShowLevelByPartnerId(currentHeroData.partnerId)
    this.refresh();
end

function UICharactorInfoHeroSkill.addEvent()
    trickGroup.btn.onClick:Add(this.onClickShowTrickSkill)
    specialGroup.btn.onClick:Add(this.onClickShowSpecialSkill)
    specialTwoGroup.btn.onClick:Add(this.onClickShowSpecialTwoSkill)
    specialTwoGroup.btnSwitch.onClick:Add(this.onClickSwitchSpecialTwo)
    passiveGroup.btn.onClick:Add(this.onClickShowPassiveSkill)
    passiveGroup.btnSwitch.onClick:Add(this.onClickShowPassiveSkill)
    skill5Group.btn.onClick:Add(this.onClickShowSkill5)

end

function UICharactorInfoHeroSkill.removeEvent()
    trickGroup.btn.onClick:Remove(this.onClickShowTrickSkill)
    specialGroup.btn.onClick:Remove(this.onClickShowSpecialSkill)
    specialTwoGroup.btn.onClick:Remove(this.onClickShowSpecialTwoSkill)
    specialTwoGroup.btnSwitch.onClick:Remove(this.onClickSwitchSpecialTwo)
    passiveGroup.btn.onClick:Remove(this.onClickShowPassiveSkill)
    passiveGroup.btnSwitch.onClick:Remove(this.onClickShowPassiveSkill)
    skill5Group.btn.onClick:Clear()
end

function UICharactorInfoHeroSkill.LocalListenEvent()
    ListenEvent(Config.EventType.Fresh_Skill,this.reGetData)
    ListenEvent(Config.EventType.Fresh_Star,this.reGetData)
end

function UICharactorInfoHeroSkill.LocalCloseEvent()
    CloseEvent(Config.EventType.Fresh_Skill,this.reGetData)
    CloseEvent(Config.EventType.Fresh_Star,this.reGetData)
end

function UICharactorInfoHeroSkill.OnDestroy()
    this.removeEvent();
end

function UICharactorInfoHeroSkill.OnEnable()
    this.LocalListenEvent();
end

function UICharactorInfoHeroSkill.OnDisable()
    this.LocalCloseEvent();
end

function UICharactorInfoHeroSkill.reGetData()
    if currentHeroData~=nil then
        currentHeroData = logicMgr.HeroManager.getHeroDatabyPartnerId(tostring(currentHeroData["partnerId"]),true);
        this.refresh();
    end
end

function UICharactorInfoHeroSkill.refresh()
    Util.SetPlayerPrefs(logicMgr.HeroManager.GetRoleInfo().playerId..Config.EventType.Fresh_HeroSkill..currentHeroData.partnerModelId, 0);
    FireEvent(Config.EventType.Fresh_HeroSkill);
    this.ctrlRace.selectedIndex = currentHeroData.faction
    local skillInfos= logicMgr.SkillManager.getSkillInfosById(currentHeroData.partnerModelId);
    this.refreshSkillIcon(skillInfos[EnumConst.SkillFieldType.Trick],trickGroup);
    this.refreshSkillIcon(skillInfos[EnumConst.SkillFieldType.Speical],specialGroup);
    this.refreshSkillIcon(skillInfos[EnumConst.SkillFieldType.SpecialTwo],specialTwoGroup);
    this.refreshSkillIcon(skillInfos[EnumConst.SkillFieldType.Passive],passiveGroup);
    this.refreshSkillIcon(skillInfos[EnumConst.SkillFieldType.skill5],skill5Group);
    if skillInfos[EnumConst.SkillFieldType.skill5] then
        this.ctrlFifthSkill.selectedIndex = 1
    else
        this.ctrlFifthSkill.selectedIndex = 0
    end
end

function UICharactorInfoHeroSkill.refreshSkillIcon(skillInfoItem,curGroup)

    if skillInfoItem == nil then
        curGroup["skill"] = nil;
        curGroup["level"] = 0;
        curGroup["btn"].visible = false
        return;
    end

    curGroup["btn"].visible = true

    local curLv = logicMgr.SkillManager.getSkillLv(skillInfoItem,this.level); --当前等级
    
    if curGroup.partnerId == currentHeroData.partnerId and curGroup.level < curLv then
        AudioManager.Play(2024)
    end
    
    curGroup["skill"] = skillInfoItem;
    curGroup["level"] = curLv;
    curGroup.partnerId = currentHeroData.partnerId
    local unlock = curLv>0;
    curGroup.objLock:SetSelectedIndex(not unlock and 1 or 0)
    curGroup.objLv:SetSelectedIndex(unlock and 1 or 0)
    if curLv>0 then
        curGroup.txtLv.text = tostring(curLv);
    else
        local str = G.dataTable["t_skillunlocker"][tostring(skillInfoItem.f_SkillID)].f_SkillUnlocker
        curGroup.txtLock.text = string.format(getLanguage("Sys_UnlockHeroRank"),str);

    end
    if curGroup["btnSwitch"]~=nil then
        curGroup.btnSwitch.visible = false
    end

    local iconName = tostring(skillInfoItem["f_SkillIcon"]);
    curGroup.icon.url = "ui://SkillAtlas/"..iconName
end

function UICharactorInfoHeroSkill.onClickShowTrickSkill()
    if trickGroup["skill"]~=nil then
        showUI(PanelNames.UICharactorInfoHeroSkillDetails,trickGroup["skill"],trickGroup["level"],this.level);
        FireEvent(Config.EventType.Click_HeroSkill, trickGroup["skill"].f_SkillID)
        AudioManager.PlayInteractionVoice(this.currentHeroData["partnerModelId"],0,AudioManager.InteractionTagTable[12].Tag)
    end
end


function UICharactorInfoHeroSkill.onClickShowSpecialSkill()
    if specialGroup["skill"]~=nil then
        showUI(PanelNames.UICharactorInfoHeroSkillDetails,specialGroup["skill"],specialGroup["level"],this.level);
        FireEvent(Config.EventType.Click_HeroSkill, specialGroup["skill"].f_SkillID)
        AudioManager.PlayInteractionVoice(this.currentHeroData["partnerModelId"],0,AudioManager.InteractionTagTable[13].Tag)
    end
end


function UICharactorInfoHeroSkill.onClickShowSpecialTwoSkill()
    if specialTwoGroup["skill"]~=nil then
        showUI(PanelNames.UICharactorInfoHeroSkillDetails,specialTwoGroup["skill"],specialTwoGroup["level"],this.level);
        FireEvent(Config.EventType.Click_HeroSkill, specialTwoGroup["skill"].f_SkillID)
        AudioManager.PlayInteractionVoice(this.currentHeroData["partnerModelId"],0,AudioManager.InteractionTagTable[14].Tag)
    end
end


function UICharactorInfoHeroSkill.onClickSwitchSpecialTwo()
    if #logicMgr.SkillManager.getImitationSkillInfos(EnumConst.SkillFieldType.SpecialTwo)>0 then
        showUI(PanelNames.UICharactorInfoHeroSkillSelect,specialTwoGroup["skill"],EnumConst.SkillFieldType.SpecialTwo);
        AudioManager.PlayInteractionVoice(this.currentHeroData["partnerModelId"],0,AudioManager.InteractionTagTable[14].Tag)
    else
        UISysTips.AddMsg(getLanguage("Sys_MsgUnableMocking"));
    end
end


function UICharactorInfoHeroSkill.onClickShowPassiveSkill()
    if passiveGroup["skill"] then
        showUI(PanelNames.UICharactorInfoHeroSkillDetails,passiveGroup["skill"],passiveGroup["level"],this.level);
        FireEvent(Config.EventType.Click_HeroSkill, passiveGroup["skill"].f_SkillID)
        AudioManager.PlayInteractionVoice(this.currentHeroData["partnerModelId"],0,AudioManager.InteractionTagTable[15].Tag)
    else
        UISysTips.AddMsg(getLanguage("Sys_MsgUnableMocking"));
    end
end

function UICharactorInfoHeroSkill.onClickSwitchPassive()
    if #logicMgr.SkillManager.getImitationSkillInfos(EnumConst.SkillFieldType.Passive)>0 then
        showUI(PanelNames.UICharactorInfoHeroSkillSelect,passiveGroup["skill"],EnumConst.SkillFieldType.Passive);
        AudioManager.PlayInteractionVoice(this.currentHeroData["partnerModelId"],0,AudioManager.InteractionTagTable[15].Tag)
    else
        UISysTips.AddMsg(getLanguage("Sys_MsgUnableMocking"));
    end
end

return UICharactorInfoHeroSkill;
